package com.juzipi.framework.config;

import com.juzipi.framework.web.service.MyUserDetailsService;
import com.juzipi.framework.security.filter.JwtAuthenticationTokenFilter;
import com.juzipi.framework.security.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailsService myUserDetailsService;
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;



    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //退出，登录，发送验证码，校验验证码
                .antMatchers("/login/**",//登录
                        "/logout/**",//退出
                        "/account/sendCode/**",//发送验证码(找回密码)
                        "/account/getRegisterCode/**",//发送注册验证码
                        "/account/checkCode/**",//验证验证码
                        "/account/checkEmailAndCode/**",//验证邮箱和验证码
                        "/account/register/**"//注册
                        ).permitAll()//放行登录退出
                .antMatchers("/**/swagger-ui/**").permitAll()
                .anyRequest().authenticated()
                .and().formLogin()
                .successHandler(getMyAuthenticationSuccessHandler())//成功
                .failureHandler(getMyAuthenticationFailureHandler())//失败
                .and().exceptionHandling()
                .authenticationEntryPoint(getMyAuthenticationEntryPoint())//未登录
                .accessDeniedHandler(getMyAccessDeniedHandler())//权限不足
                .and().logout().permitAll()
                .logoutSuccessHandler(getMyLogoutSuccessHandler())//退出成功
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //关闭csrf防护
        http.csrf().disable()
                //使用token不需要session，关闭
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService)
                .passwordEncoder(getPasswordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }


    /**
     * 不写这个会报一个错误，我也忘了是什么了
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }







    //密码验证
    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }


    /**
     *登录成功自定义处理
     * @return JSON
     */
    @Bean
    public MyAuthenticationSuccessHandler getMyAuthenticationSuccessHandler(){
        return new MyAuthenticationSuccessHandler();
    }


    /**
     * 登陆失败自定义处理
     * @return JSON
     */
    @Bean
    public MyAuthenticationFailureHandler getMyAuthenticationFailureHandler(){
        return new MyAuthenticationFailureHandler();
    }


    @Bean
    public MyAuthenticationEntryPoint getMyAuthenticationEntryPoint(){
        return new MyAuthenticationEntryPoint();
    }


    @Bean
    public MyAccessDeniedHandler getMyAccessDeniedHandler(){
        return new MyAccessDeniedHandler();
    }


    @Bean
    public MyLogoutSuccessHandler getMyLogoutSuccessHandler(){
        return new MyLogoutSuccessHandler();
    }

}
